<?php
namespace app\index\controller;
use think\Controller;
use Spread\Spread;
class Index extends Controller
{
    // --- 首页
    public function index(){
        return view();
    }

    // --- Excel导出
    public function put(){
		$Excel['fileName']="ThinkPHP导出spreadsheet示例-".date('Y年m月d日-His',time());//or $xlsTitle
		$Excel['cellName']=['A','B','C','D'];
		$Excel['H'] = ['A'=>12,'B'=>20,'C'=>14,'D'=>16];//横向水平宽度
		$Excel['V'] = ['1'=>40,'2'=>23];//纵向垂直高度
		$Excel['sheetTitle']="spreadsheet示例";//大标题，自定义
		$Excel['xlsCell']=[['autoid','序号'],['school','学校'],['addr','省份'],['type','类型']];
        Spread::excelPut($Excel,$this->getData());
    }

    // --- Excel读取
    public function read(){
        if(request()->file("file")){
            $file = request()->file('file');
            $info = $file->move( './uploads/excel');
            if($info){
                $path = "./uploads/excel/".$info->getSaveName();
                $ret = Spread::excelReader($path);
                if($ret){
                    return json(['state'=>1,'data'=>$ret]);
                }else{
                    return json(['state'=>0,'msg'=>'处理错误']);
                }
            }else{
                return json(['state'=>0,'msg'=>'文件不存在']);
            }
        }
        return view();
    }

    // --- IP转区域
    public function ipregion(){
        if(request()->isPost()){
	        $line = input("post.line");
	        return model("IpRegion")->getRegionByIp($line);
        }else{
            $this->assign("myip",request()->ip(0,true));
        	return view();
        }
    }

    // --- 模拟数据库
    protected function getData(){
        return [
            ['autoid'=>'1','school'=>'云南大学','addr'=>'云南省','type'=>'综合'],
            ['autoid'=>'2','school'=>'云南财经大学','addr'=>'云南省','type'=>'财经'],
            ['autoid'=>'3','school'=>'云南民族大学','addr'=>'云南省','type'=>'综合'],
            ['autoid'=>'4','school'=>'云南师范大学','addr'=>'云南省','type'=>'师范'],
            ['autoid'=>'5','school'=>'云南旅游大学','addr'=>'云南省','type'=>'综合'],
            ['autoid'=>'6','school'=>'贵州大学','addr'=>'贵州省','type'=>'综合'],
            ['autoid'=>'7','school'=>'贵州财经大学','addr'=>'贵州省','type'=>'财经'],
            ['autoid'=>'7','school'=>'贵州师范大学','addr'=>'贵州省','type'=>'师范']
            ];
    }
}
